package com.youga.xuapplication.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.youga.xuapplication.R;
import com.youga.xuapplication.config.HttpConstant;

import net.cgsoft.widget.AlertDialogFragment;
import net.cgsoft.widget.DownFragment;
import net.cgsoft.widget.ItemDialogFragment;
import net.cgsoft.widget.LoadFragment;
import net.cgsoft.widget.PickerFragment;
import net.cgsoft.widget.TimeFragment;

import java.util.Date;

/**
 * Created by Youga on 2016/1/13.
 */
public class BaseActivity extends AppCompatActivity implements HttpConstant {

    protected final String TAG = getClass().getSimpleName();
    protected AppCompatActivity mContext;
    private boolean mDestroyed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
    }


    protected void showToast(String text) {
        if (text != null && !TextUtils.isEmpty(text.trim()) && !"null".equals(text.trim()))
            Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
    }

    protected void showToast(int text) {
        Toast.makeText(mContext, getString(text), Toast.LENGTH_LONG).show();
    }

    protected void showProgressDialog(int msg) {
        showProgressDialog(getString(msg));
    }

    protected void showProgressDialog() {
        showProgressDialog("加载中,请稍后...");
    }

    protected void showProgressDialog(String msg) {
        recyclerFragment(LoadFragment.TAG);
        LoadFragment fragment = LoadFragment.newInstance(msg);
        fragment.show(getSupportFragmentManager(), ItemDialogFragment.TAG);
    }

    protected void dismissProgressDialog() {
        LoadFragment fragment = (LoadFragment) getSupportFragmentManager().findFragmentByTag(LoadFragment.TAG);
        if (fragment != null) fragment.dismiss();
    }

    public void showHierarchyLoadDialog(int containerViewId, String type, String tips) {
//        recyclerFragment(containerViewId);
//        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//        LoadFragment fragment = LoadFragment.newInstance(type, tips, new int[]{
//                R.drawable.login_icon, R.drawable.empty, R.drawable.no_network});
//        ft.replace(containerViewId, fragment);
//        if (this.mDestroyed) return;
//        ft.commitAllowingStateLoss();
    }

    public void showHierarchyFragment(Fragment fragment, int containerViewId) {
        recyclerFragment(containerViewId);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(containerViewId, fragment);
        if (this.mDestroyed) return;
        ft.commitAllowingStateLoss();
    }

    public void showAlertDialog(boolean cancelable,
                                AlertDialogFragment.AlertDialogFragmentCallBack callBack,
                                String... text) {
        recyclerFragment(AlertDialogFragment.TAG);
        AlertDialogFragment fragment = AlertDialogFragment.newInstance(cancelable, callBack, text);
        fragment.show(getSupportFragmentManager(), AlertDialogFragment.TAG);
    }

    public void showItemDialog(String title, String[] items,
                               ItemDialogFragment.ItemDialogFragmentCallBack callBack) {
        recyclerFragment(ItemDialogFragment.TAG);
        ItemDialogFragment fragment = ItemDialogFragment.newInstance(callBack, title, items);
        fragment.show(getSupportFragmentManager(), ItemDialogFragment.TAG);
    }

    public void showDownDialog(String url, String title) {
        recyclerFragment(DownFragment.TAG);
        DownFragment fragment = DownFragment.newInstance(url, title);
        fragment.show(getSupportFragmentManager(), DownFragment.TAG);
    }

    public void showPickerDate(Date date, PickerFragment.PickerFragmentCallBack callBack) {
        recyclerFragment(PickerFragment.TAG);
        PickerFragment fragment = PickerFragment.newInstance(date, callBack);
        fragment.show(getSupportFragmentManager(), PickerFragment.TAG);
    }

    public void showPickerTime(TimeFragment.TimeFragmentCallBack callBack) {
        recyclerFragment(TimeFragment.TAG);
        TimeFragment fragment = TimeFragment.newInstance(callBack);
        fragment.show(getSupportFragmentManager(), TimeFragment.TAG);
    }

    public void recyclerFragment(String tag) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag(tag);
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        if (this.mDestroyed) return;
        ft.commitAllowingStateLoss();
    }

    public void recyclerFragment(int id) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentById(id);
        if (prev != null) {
            Log.i(TAG, "recyclerFragment:" + prev.getClass().getSimpleName());
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        if (this.mDestroyed) return;
        ft.commitAllowingStateLoss();
    }


    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.mDestroyed = true;
    }
}
